/**
 * Created by yangfan on 2016/8/29.
 */
define(["require",'config/appregister',
    "../../services/httpServices",
    "business/library/services/httpServices",
    "i18n",
    "business/services/utils",
    "business/services/constant",
    'business/services/moreHttpServices',
    "business/personMine/services/filter","business/home/directives/share","jquery"
],function(require,app,httpService,libHttpServices,i18n,utils,constant,moreHttpServices){
    app.controller('libraryDetailCtrl',function($scope,$stateParams,$q,$compile,$anchorScroll,$location,$state,auth,$rootScope,$http){
    	$scope.share = {
    		url:"www.baidu.com",
    		title:'你好啊',
    		summary:'我不是很好'
    	}
    	
        $scope.$parent.isDetailPage = true;
        $scope.$parent.isMainpage = false;
        $scope.i18n = i18n;
        $scope.textarea ="";
        $scope.draw = {
            userPngSrc:"them/images/123.jpg",
            label:["别墅设计","施工图纸","建筑图纸"],
            setStyle:function(index){
                return {"border":"1px solid"+"red"}
            },
            content:{
                title:"内容简介"+":",
                des:"本图纸为:A城市三层独栋别墅建筑设计施工图纸,内容包括平面图,剖面图,东立面图等,内容详实,仅供参考。",
                list:{
                    "villaType":{
                        key:"别墅类型",
                        value:"独栋别墅"
                    },
                    "highType":{
                        key:"高度类别",
                        value:"高层建筑"
                    },
                    "strucType":{
                        key:"结构形式",
                        value:"钢筋混泥土结构",
                    },
                    "gjhntStruc":{
                        key:"钢筋混泥土结构",
                        value:"框架"
                    },
                    "drawDepth":{
                        key:"图纸深度",
                        value:"施工图"
                    },
                    "proPosition":{
                        key:"项目位置",
                        "value":"浙江"
                    },
                    "drawType":{
                        key:"图纸格式",
                        value:"CAD2000"
                    },
                    "drawNum":{
                        key:"图纸张数",
                        value:"15张"
                    },
                    "buildHigh":{
                        key:"建筑高度",
                        value:"15米"
                    },
                    "overgroundClass":{
                        key:"地上层数",
                        value:"3层"
                    },
                    "effectDraw":{
                        key:"效果图",
                        value:"无"
                    }
                }
            },
            download:{
                fileType:"("+$scope.i18n.pdf+")",
                downloadNeed:5+"积分",
                uploadTime:"2016-7-1",
            },
            comment:{
                allComment:$scope.i18n.allComment ,
                commentNum:"("+132+")",
                placehoder:"写下你的评论......",
            }
        };
        //文件预览
        function getPreviewPngOfFile(){
            var req = {
                type: "GET",
                uri:'getPreviewPngOfFile',
                data:{
                    fileId :$scope.fileId,
                    type:2
                },
                removeCookie:true
            };
            httpService.getPreviewPng(req).success(function (response) {
                if (!response.data) {
                    return
                }
                $scope.$apply(function(){
                    $scope.getPreviewList = response.data.path.split('#');
                    $scope.getPreview = response.data.pageCount;
                })
            }).error(function () {
            })
        }
        $scope.dialogModel = {
            content:"想要查看更多内容请登录！",
            isShow:false,
            confirm:function(){
                $state.go("login",{type:"logion"})
            }
        }

        $scope.scroll = function(){
            console.log('a')
            $('#page').scroll(function(){
                console.log('a')
            })
        }

        function getLike (){
            var req = {
                type:"GET",
                data:{
                    thesisId :$scope.fileId,
                    count:6
                },
                uri:"getLike"
            };
            libHttpServices.getLike(req).success(function(response){
                if(!response.data) return;
                $scope.$apply(function(){
                    $scope.youLike = response.data;
                    if(response.data.length>5){
                        $scope.youLike = $scope.youlike.splice(0,5);
                    }
                })
            });
        }
        function getPreView(url){
            var req = {
                type:"POST",
                data:{
                    downloadUrl:url,
                    covertType:10
                },
                removeCookie:true
            };
            libHttpServices.getPreview(req).success(function(response){
                $scope.$apply(function(){

                })

                $scope.previewList = response.data ;
            }).error(function(){})
        };
        function getFile (){
            var req = {
                type:"GET",
                data:{
                    thesisId :$scope.fileId
                },
                uri:"get"

            };
            libHttpServices.getThesis(req).success(function(response){
                if(!response.data)return;
                $scope.$apply(function(){
                	//console.log(response)
                    if(response.data.file.flag){
                        $scope.collectionTitle = "已收藏";
                        $scope.isCollected = true;
                    }else{
                        $scope.collectionTitle = "收藏";
                        $scope.isCollected = false;
                    }
                    $scope.cate = response.data.category;
                    $scope.draws = response.data.file;
                    $scope.draws.reference = getReference(response.data.file.reference);
                    $scope.content = utils.contentConvert(response.data.file.introduction);
                    $scope.draws.size = response.data.file.size ? response.data.file.size/1024:'';
                    $scope.draws.downloadCount = response.data.file.downloadCount;
                });
            }).error(function(){
            });
        }
        function getReference(reference){
            var Arr = [];
            if(!reference) return ;
                if(reference.indexOf('ㅆ')<0){
                    Arr.push(reference);
                    return Arr
                }else{
                    Arr = reference.split('ㅆ');
                    return Arr
                }
        }
        // 共享资料
        function getShare(){
            var req = {
                type:"GET",
                data:{
                    thesisId :$scope.fileId
                },
                uri:"getShare"
            };
            libHttpServices.getThesis(req).success(function(response){
                $scope.$apply(function(){
                    $scope.share = response.data.share ;
                    if($scope.share.length > 7){
                        $scope.share = $scope.share.splice(0,7);
                    }
                    $scope.shareCount = response.data.shareCount ;
                })
            })
        }


        //获取下载排行数据
        function getDownLoadRank() {
            var req = {
                type: "GET",
                data:{
                    count:6
                },
                uri:"getDownLoadRank"
            };
            libHttpServices.getDownLoadRank(req).success(function (response) {
                if (!response.data) {
                    return
                }
                $scope.downloadRank = response.data;
                getDownLoadRankConf($scope.downloadRank);
            }).error(function () {
            })
        }

        function getDownLoadRankConf(data){
            $scope.setConf = {
                data:data,
                idType:"thesisId",
                state:"eng.libraryDetail"
            }
        }

        //获取一资分类
        function getRelevantType() {
            if (!$scope.fileId) {
                return
            };
            var req = {
                type: "GET",
                data: {
                    thesisId: $scope.fileId,
                    count: 6
                },
                uri:"getRelevantType"
            };
            libHttpServices.getRelevantType(req).success(function (response) {
                if (!response.data) {
                    return
                }
                $scope.relevantType = response.data;
            }).error(function () {
            })
        }
        //获取相关推荐
        function getRelevant() {
            if (!$scope.fileId) {
                return
            };
            var req = {
                type: "GET",
                uri:"getRelevant",
                data: {
                    thesisId: $scope.fileId,
                    count: 6
                }
            };
            libHttpServices.getRelevant(req).success(function (response) {
                if (!response.data) {
                    return
                }
                $scope.relevant = response.data;
            }).error(function () {
            });
        }

        //更新收藏
        function updateCollection(){
            moreHttpServices.updateCollection($scope.fileId,5).success(function(response){
                if(response.code == 0){
                    $scope.$apply(function(){
                        $scope.collectionTitle = "已收藏";
                        $scope.isCollected = true;
                    })
                }
                if(response.code == 112){
                    $scope.$apply(function(){
                        $scope.collectionTitle = "收藏";
                        $scope.isCollected = false;
                    })
                }
            }).error(function(){

            })
        }
        //初始化
        if(!angular.isUndefined($stateParams.thesisId)){
           // var deferred = $q.defer();
            $scope.fileId= $stateParams.thesisId;
            getFile ();
            getShare();
            getPreviewPngOfFile();
            getLike ();
            getDownLoadRank();
            getRelevantType();
            getRelevant();
        };
/*
* 评论类型1--博文，2--企业，3--职位,4--资料,5--论文,6--新闻,7--技能,8--课程,9--视频,默认1
* */
        $scope.commentConf = {
            showStar: false,
            conf: {
                id: $stateParams.thesisId,
                type: 5
            }
        };

        //获取面包树导航
        $scope.breadPath = ["工程资料库"];
        if(localStorage.scName && localStorage.dcName&& localStorage.title){
            $scope.breadPath.push(localStorage.scName);
            $scope.breadPath.push(localStorage.dcName);
            $scope.breadPath.push(localStorage.title);
        };

        $scope.hideAngle = function(index){
            if(index == $scope.breadPath.length-1){
                return true
            }else{
                return false
            }
        };
        $scope.drawContent = $scope.draw.content.list;
        $scope.isCollected = false;
        $scope.collectionTitle = "收藏";
        $scope.updateCollection = function(){
            auth.isLogin().then(function(){
                updateCollection();
            })
        };
        $scope.goto =function(id){
            $("html,body").animate({scrollTop: $("#"+id)[0].offsetTop - 135}, 500);
        };
        $scope.stateGo =function(state){
            $state.go(state);
        };

        // 点击资料 显示下拉框
        $scope.isShowData = false;
        $scope.clickData = function(){
            if($scope.isShowData == false){
                $scope.isShowData = true;
            }
        };
        $scope.leaveSearchList =function(){
            if($scope.isShowData == true){
                $scope.isShowData = false;
            }
        };

        // 点击资料 显示下拉框
        $scope.isShowData = false;
        $scope.isInList = false;
        $scope.clickData = function(){
            if($scope.isShowData == false){
                $scope.isShowData = true;
            }
        };
        $scope.leaveSearchList =function(){
            if($scope.isShowData == true){
                $scope.isShowData = false;
            }
            $scope.isInList = true;
        };
        
        //详情
        $scope.goDetail = function(item){
        	$state.go('eng.libraryDetail',{'thesisId':item.thesisId},{reload:true})
        };
        //三级里面没以及 所以传topItem
        $scope.searchMain = function(item,type){
            if(!type){
                return
            }
            var obj = {
                tcId:1,
                scId:"",
                dcId:""
            };
            if(type == "top"){
                obj.tcId = 1;
            }
            if(type == "sec"){
                obj.scId = 1 ;
                obj.tcId = item.tcId;
            }
            if(type == "detail"){
                obj.scId = 1 ;
                obj.tcId = item.tcId;
                obj.dcId = item.dcId;
            }
            var str = JSON.stringify(obj);
            sessionStorage.engParams = str;
            $state.go('eng.libraryMain');
            
            
	        }
        //下载论文
        $scope.downloadThesis = function(){
            auth.isLogin().then(function(){
                var URL = "http://"+allUrl+":8084";
                var API = "elink_library/api/thesis";
                var URI = "download";
                window.open(URL+"/"+API+"/"+URI+"?thesisId="+$stateParams.thesisId+"&seed="+ localStorage.seed + "&authToken=" + localStorage.authToken,"_blank");
            })
        };
        $scope.goToListPage = function(item){
            var obj = {
                tcId:$scope.draws.tcId,
                scId:item.scId,
                dcId:item.dcId
            };
            var str = JSON.stringify(obj);
            sessionStorage.engParams = str;
            $state.go('eng.main');
        }
        //点击标签 调至搜索模块
        $scope.jumpToSearchPage = function(item){
            $rootScope.GLOBALKEYWORD =  item;
            sessionStorage.searchKeyWord = $rootScope.GLOBALKEYWORD;
            $state.transitionTo('search.library', {}, {
                reload: true, inherit: false, notify: true
            });
        };

        //三级里面没以及 所以传topItem
        $scope.searchMain = function(item,type,topItem){
            if(!type){
                return
            }
            var obj = {
                tcId:3,
                scId:"",
                dcId:""
            };
            if(type == "top"){

            }
            if(type == "sec"){
                obj.scId = item.scId ;
            }
            if(type == "detail"){
                obj.scId = item.scId ;
                obj.dcId = item.dcId;
            }
            var str = JSON.stringify(obj);
            sessionStorage.engParams = str;
            $state.go('eng.libraryMain');
        }

        $scope.jumpToUsersCenter = function(item){
            if(item.userId == $rootScope.userLoginInfo.data.userId){
                $state.go('myMainPage',{userId:item.userId})
            }else{
                $state.go('othersMainPage',{userId:item.userId})
            }
        }


    })
});
